package com.tb.education.server;

import com.tb.education.common.enumeration.OperationTypeEnum;
import com.tb.education.common.intf.AbsBaseHandler;
import com.tb.education.common.packets.DevicePacket;
import com.tb.education.handler.DeviceLoginHandler;
import com.tb.education.handler.HeartBeatHandler;
import com.tb.education.handler.TemperHandler;
import org.rockyang.tio.common.starter.annotation.TioMsgHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.ChannelContext;
import org.tio.core.intf.Packet;


import java.util.HashMap;
import java.util.Map;


/**
 * 消息处理实现类
 *
 * @author wangze
 * @date 2020-07-08 14:37.
 **/
@TioMsgHandler
public class ServerMsgHandler extends ServerAbsMsgHandler {

	private static Logger log = LoggerFactory.getLogger(ServerMsgHandler.class);

	private static Map<String, AbsBaseHandler> handlerMap = new HashMap<>();

	static {
		handlerMap.put(OperationTypeEnum.DEVICE_LOGIN.getKey(), new DeviceLoginHandler());
		handlerMap.put(OperationTypeEnum.REPORT_HEARTBEAT.getKey(), new HeartBeatHandler());
		handlerMap.put(OperationTypeEnum.REPORT_TEMPER.getKey(), new TemperHandler());
	}

	/**
	 * 处理消息
	 */
	@Override
	public void handler(Packet packet, ChannelContext channelContext) throws Exception {
		DevicePacket devicePacket = (DevicePacket) packet;
		AbsBaseHandler handler = handlerMap.get(devicePacket.getOperationType());
		if (handler == null) {
			log.error("{}, 找不到处理类，type:{}", channelContext, devicePacket.getOperationType());
			return;
		}
		handler.handler(devicePacket, channelContext);
		return;
	}

}
